package zong.Listener;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class test2 extends JFrame {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setSize(500, 500);
        jFrame.setTitle("事件演示");
        jFrame.setAlwaysOnTop(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLayout(null);


        //创建按钮对象
        JButton jtb1=new JButton("点我啊");
        jtb1.setBounds(10, 10, 100, 30);
       /* jtb1.addActionListener(new MyActionListener() );//监听鼠标左键单击和空格


*/

        //匿名内部类
       jtb1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("按钮被点击了");
            }
        });

        jFrame.getContentPane().add(jtb1);//把按钮添加到界面






        jFrame.setVisible(true);
    }
}
